package com.thread;

public class SellTicket {
    public static void main(String[] args) {
//        T1 t1 = new T1();
//        T1 t2 = new T1();
//        T1 t3 = new T1();

//        t1.start();
//        t2.start();
//        t3.start();
        T2 t1 = new T2();
        Thread thread1 = new Thread(t1);
        Thread thread2 = new Thread(t1);
        Thread thread3 = new Thread(t1);
        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class T1 extends Thread{
    private int sticknum = 100;
    @Override
    public void run() {
        while (true){
            if (sticknum < 1){
                System.out.println("票卖完了");
                return;
            }
            else{
                try {
                    Thread.sleep(500);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                System.out.println("窗口"+Thread.currentThread().getName()+"出售一张票，"+"还有"+(--sticknum));
            }
        }
    }
}

class T2 implements Runnable{
    private static int sticknum = 100;
    @Override
    public void run() {
        while (true){
            if (sticknum <= 0){
                System.out.println("结束");
                return;
            }
            try{
                Thread.sleep(50);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("窗口"+Thread.currentThread().getName()+"正在出售一张票"+"，剩余"+(--sticknum));
        }

    }
}
